﻿using UnityEngine;
using System.Collections;

/// <summary>
/// 游戏流程
/// </summary>
public class GameProceduar : BaseProceduar {

    ///////////////////////////////////////////////////////////////////////////
    /// <summary>
    /// 游戏界面UI
    /// </summary>
    UIGameWindow _ui;
    /// <summary>
    /// 当前游戏状态
    /// </summary>
    GameStateBase _curState;

    ///////////////////////////////////////////////////////////////////////////
    /// <summary>
    /// 实例
    /// </summary>
    static GameProceduar _inst;
    public static GameProceduar inst
    {
        get
        {
            if (_inst == null)
            {
                _inst = new GameProceduar();
            }
            return _inst;
        }
    }

    ///////////////////////////////////////////////////////////////////////////

    public void Switch(GameStateBase state)
    {
        if (_curState != null)
        {
            _curState.Leave();
        }

        _curState = state;

        if (_curState != null)
        {
            _curState.Enter();
        }
    }

    public override void EnterProceduar(bool initUI)
    {
        DebugInfo.Log("进入游戏流程");

        Switch(new GameStateInit());
    }

    public override void UpdateProceduar()
    {
        base.UpdateProceduar();
    }

    public override void LeaveProceduar()
    {
        DebugInfo.Log("退出游戏流程");
    }

    protected override bool ProcessPacket(IOMessage message)
    {
        // 状态处理数据包
        if (_curState.ProcessPacket(message))
        {
            return true;
        }

        // 基类处理数据包
        return base.ProcessPacket(message);
    }

    /// <summary>
    /// 初始化UI
    /// </summary>
    public void InitUI()
    {
        UIJoinRoomWindow.CloseWindow();
        UICreateRoomWindow.CloseWindow();

        _ui = new UIGameWindow();
        UIRootController.inst.SwitchTo(_ui);
    }
}
